package com.gln.bloger.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.gln.bloger.pojos.Entry;
import com.gln.bloger.pojos.Tag;
import com.gln.bloger.util.PagingSupport;

/**
 * entry for user
 * Created on 20080115
 * @author renxj
 * modify entries/articles 
 */
public class TagAction extends GuestBaseAction {
	
	/**
	 * list tags 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward listTags(ActionMapping mapping, ActionForm form,
		   HttpServletRequest request, HttpServletResponse response) 
	       throws Exception{
		List<Tag> tags = getTagService().getTags(getAuthWebsite(request));
		int totalCount = 0;
		
		for(Tag t : tags){
			totalCount +=t.getCount();
		}
		request.setAttribute("tags", tags);
		request.setAttribute("totalCount", totalCount);
		return mapping.findForward("listTags");
	}
	
	/**
	 * list tags 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward list(ActionMapping mapping, ActionForm form,
		   HttpServletRequest request, HttpServletResponse response) 
	       throws Exception{
		String name = request.getParameter("name");
		String p = request.getParameter("p");
		Tag tag = getTagService().getTagByName(getAuthWebsite(request), name);
		PagingSupport<Entry> entries = getEntryService().
		               getEntriesByTag(getAuthWebsite(request),tag.getId());
		if(p != null){
			entries.setCurrentPage(Integer.parseInt(p));
		}
		request.setAttribute("tag", tag);
		request.setAttribute("entries", entries);
		return mapping.findForward("listEntries");
	}
}
